"""
用 for 循环实现 1 ~ 100 求和
range(101)：可以用来产生0到100范围的整数，需要注意的是取不到101。
range(1, 101)：可以用来产生1到100范围的整数，相当于前面是闭区间后面是开区间。
range(1, 101, 2)：可以用来产生1到100的奇数，其中2是步长，即每次数值递增的值。
range(100, 0, -2)：可以用来产生100到1的偶数，其中-2是步长，即每次数字递减的值。
"""
import random
import math

s = 0
for i in range(1, 100):
    s += i
print('s = ', s)

s = 0
for i in range(1, 100, 2):
    s += i
print('s = ', s)

s = 0
for i in range(2, 101, 2):
    s += i
print('s = ', s)
print('\n')

"""
输入九九乘法口诀表
"""
for i in range(1, 10):
    for j in range(1, i + 1):
        print('%d * %d = %d' % (i, j, i * j), end='\t')
print('\n')

counter = 0
answer = random.randint(1, 100)

# while True:
#     counter += 1
#     i = int(input('请输入：'))
#     if i > answer:
#         print('smaller')
#     elif i < answer:
#         print('bigger')
#     else:
#         print('Congratulations!')
#         break
# print('你一共猜了 %d 次' % counter)
# if counter > 7:
#     print('你的智商有问题')
# print('\n')

"""
判断一个整数是不是素数
sqrt 用来计算一个数的平方根
"""
num = 57
end = int(math.sqrt(num))
is_prime = True
if num < 1:
    is_prime = False
else:
    for i in range(2, end + 1):
        if num % i == 0:
            is_prime = False
            break
if is_prime:
    print('%d is a prime number' % num)
else:
    print('%d is not a prime number' % num)

"""
计算连个正整数的最大公约数和最小公倍数
"""
x = 5
y = 3
if x > y:
    x, y = y, x
for factor in range(x, 0, -1):
    if x % factor == 0 and y % factor == 0:
        print('the greatest common divisor of %d and %d is %d' % (x, y, factor))
        print('the least common multiple of %d and %d is %d' % (x, y, x * y // factor))
        break
print('\n')

"""
打印三角形
"""
row = 5
for i in range(row):
    for j in range(i + 1):
        print('*', end='')
    print()

for i in range(row):
    for j in range(row):
        if j < row - i - 1:
            print(' ', end='')
        else:
            print('*', end='')
    print()

for i in range(row):
    for _ in range(row - i - 1):
        print(' ', end='')
    for _ in range(2 * i + 1):
        print('*', end='')
    print()

